package com.ambow.teaching.view;

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.ambow.teaching.dao.UserDAO;
import com.ambow.teaching.entity.User;

/**
 * 添加用户框架。
 * 
 * @author deng.zhao
 *
 */
public class AddUserFrame extends JFrame {
	// 
	private static final long serialVersionUID = -3982984070790612160L;
	// 姓名文本框；
	private JTextField nameField;
	// 密码文本框；
	private JPasswordField passwordField;

	public AddUserFrame() throws HeadlessException {
		super("添加用户");
		setSize(400, 300);
		setResizable(false);
		setLayout(new GridBagLayout());
		initComponents();
	}

	private void initComponents() {
		// 面板；
		JPanel panel = new JPanel(new GridBagLayout());
		GridBagConstraints constraintsPanel = new GridBagConstraints(1, 1, 1, 1, 100, 100, GridBagConstraints.NORTHWEST,
				GridBagConstraints.BOTH, new Insets(20, 50, 20, 50), 0, 0);
		add(panel, constraintsPanel);
		// 用户名标签；
		JLabel nameLabel = new JLabel("用户名：");
		GridBagConstraints constraintsNameLabel = new GridBagConstraints(1, 1, 1, 1, 100, 100, GridBagConstraints.WEST,
				GridBagConstraints.NONE, new Insets(5, 20, 5, 5), 0, 0);
		panel.add(nameLabel, constraintsNameLabel);
		// 用户名文本框；
		nameField = new JTextField();
		GridBagConstraints constraintsNameField = new GridBagConstraints(2, 1, 1, 1, 100, 100, GridBagConstraints.WEST,
				GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);
		panel.add(nameField, constraintsNameField);
		// 密码标签；
		JLabel passwordLabel = new JLabel("密码：");
		GridBagConstraints constraintsPasswordLabel = new GridBagConstraints(1, 2, 1, 1, 100, 100,
				GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 20, 5, 5), 0, 0);
		panel.add(passwordLabel, constraintsPasswordLabel);
		// 密码文本框；
		passwordField = new JPasswordField(16);
		GridBagConstraints constraintsPasswordField = new GridBagConstraints(2, 2, 1, 1, 100, 100,
				GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);
		panel.add(passwordField, constraintsPasswordField);
		// 确定按钮；
		JButton okButton = new JButton("添加");
		GridBagConstraints constraintsOkButton = new GridBagConstraints(1, 3, 1, 1, 100, 100, GridBagConstraints.EAST,
				GridBagConstraints.NONE, new Insets(5, 10, 5, 5), 0, 0);
		panel.add(okButton, constraintsOkButton);
		// 取消按鈕；
		JButton cancelButton = new JButton("取消");
		GridBagConstraints constraintsCancelButton = new GridBagConstraints(2, 3, 1, 1, 100, 100,
				GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0);
		panel.add(cancelButton, constraintsCancelButton);

		// 为确定按钮添加监听器；
		okButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String name = nameField.getText().trim();
				String password = new String(passwordField.getPassword());
				User user = new User(0, name, password, new Date());

				UserDAO userDao = new UserDAO();
				int rowCount;
				try {
					rowCount = userDao.add(user);
					if (rowCount == 1) {
						JOptionPane.showMessageDialog(AddUserFrame.this, "添加成功");
					} else {
						JOptionPane.showMessageDialog(AddUserFrame.this, "添加失败");
					}
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		});

		// 为取消按钮添加监听器；
		cancelButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				AddUserFrame.this.setVisible(false);
			}
		});
	}

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
				final AddUserFrame frame = new AddUserFrame();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
		});
	}

}
